//
//  NetworkTools.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit
import AFNetworking

private let weiboDomain = "com.baidu.data.error"

//线上
private let hostname = "api.weibo.com"

//枚举网络请求类型
enum HttpMethod:String{
    case Post = "post"
    case Get = "Get"
}

class NetworkTools: AFHTTPSessionManager {
    
    //定义单例对象
    static let sharedTools:NetworkTools = {
        let url = NSURL(string: hostname)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    //加载字典数据
    func requestJsonDict(method:HttpMethod,urlstring:String,parmaters:[String : AnyObject]?,finished:(result:[String:AnyObject]?,error:NSError?) ->()){
        if method == HttpMethod.Get{
            //get方法
            GET(urlstring, parameters: parmaters, success: { (_, result) in
                
                //判断回来的数据是不是字典
                if let dict  = result as? [String:AnyObject]{
                    //执行成功之后的回调
                    finished(result: dict, error: nil)
                }
                //如果回来的数据不是字典
                let dataError = NSError(domain: weiboDomain, code: -100000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                //网络访问失败的时候
                }, failure: { (_, error) in
                    finished(result: nil, error: error)
            })
            
            
        }else{
            //post方法
            POST(urlstring, parameters: parmaters, success: { (_, result) in
                
                //判断回来的数据是不是字典
                if let dict  = result as? [String:AnyObject]{
                    //执行成功之后的回调
                    finished(result: dict, error: nil)
                }
                //如果回来的数据不是字典
                let dataError = NSError(domain: weiboDomain, code: -100000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                //网络访问失败的时候
                }, failure: { (_, error) in
                    finished(result: nil, error: error)
            })

        }
        
    }
    func uploadImage(urlString:String,parmaters:[String : AnyObject]?,imageData:NSData,finished:(result:[String:AnyObject]? , error:NSError?)->()){
        POST(urlString, parameters: parmaters, constructingBodyWithBlock: { (multipartFormData) in
            //1.data  要上传的文件的二进制数据
            //2.name 表示上传的文件服务器对应的字段
            //3.filename 表示服务器接受后 存储的名字   名字可以随便取
            //4.上传文件的格式  image/jpeg
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "YDY", mimeType: "image/jpeg")
            
            }, success: { (_, result) in
                //判断数据类型
                if let dict = result as? [String : AnyObject]{
                    finished(result: dict, error: nil)
                }
            }) { (_, error) in
                finished(result: nil, error: error)
        }
    }
}
